package com.edu.servlet;

import com.edu.bean.User;
import com.edu.service.UserFeedbackService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/feedback/add")
public class UserFeedbackAddServlet extends HttpServlet {
    private UserFeedbackService feedbackService = new UserFeedbackService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 检查用户是否登录
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/user/login");
            return;
        }

        // 获取表单数据
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        // 参数验证
        if (title == null || title.trim().isEmpty() || content == null || content.trim().isEmpty()) {
            request.setAttribute("error", "标题和内容不能为空");
            request.getRequestDispatcher("/user/feedback/list.jsp").forward(request, response);
            return;
        }

        // 添加反馈
        boolean success = feedbackService.userAddFeedback(user.getUserId(), title, content);

        if (success) {
            response.sendRedirect(request.getContextPath() + "/user/feedback?success=true");
        } else {
            request.setAttribute("error", "提交失败，请稍后重试");
            request.getRequestDispatcher("/user/feedback/list.jsp").forward(request, response);
        }
    }
}